<?php
/**
 * Created by IntelliJ IDEA.
 * User: wanglu
 * Date: 17/4/28
 * Time: 11:31
 */

class  AdminBase extends MY_Controller {

    public $loginUser;
    function __construct() {
        parent::__construct();
        $this->load->helper('date');
        $this->loginUser  = $this->session->userdata('user');
    }

    public function _remap($method) {

        if (method_exists($this, $method)) {
            try {
                if ($method == 'signIn' || $method == 'sendPwd' || $method == 'password' || $method == 'sendPwd' ||  $method == 'test') {
                    $this->$method();
                } else {
                    if (!$this->checkLogin()) {
                        $this->load->view('/admin/login');
                    } else {
                        $this->$method();
                    }
                }

            } catch (AppException $e) {
                $this->response_json_width_msg($e->_code, $e->_msg, $e->_values);
            }
        } else {
            show_404();
        }
    }


    private function checkLogin() {
        return $this->session->has_userdata('logged_in');
    }

    protected function showView($view, $data = array()) {
        $data['login_user'] = $this->loginUser;
        $this->load->view('/admin/' . $view, $data);
    }

    public function is_post() {
        return strtolower($_SERVER["REQUEST_METHOD"]) == 'post';
    }

//
//    protected function getAdminUrl($url, $param = array()) {
//        if (empty($param)) {
//            return base_url("admin/".$url) . "?p=".$this->menu['p'] . "&m=". $this->menu['m'];
//        } else {
//            $url.= "?";
//
//            foreach ($param as $key => $value) {
//                $url .= $key . "=" . $value;
//                $url .= "&";
//            }
//
//            $url = substr($url,0,strlen($url)-1);
//            return base_url("admin/".$url) . "&p=".$this->menu['p'] . "&m=". $this->menu['m'];
//
//        }
//    }
//
//
//
//    public function message($type, $msg, $redirectUrl) {
//        $this->showView("message", array("type" => $type, "msg" => $msg, "redirectUrl" => $redirectUrl)) ;
//    }
//
//
//
    public function getPagination($total, $page, $param = array()) {
        $para = "&p=".$this->menu['p'] . "&m=" . $this ->menu['m'];
        if (!empty($param)) {
            $para .= "&";
        }
        
        foreach ($param as $key => $value) {
            $para .= $key . "=" . $value;
            $para .= "&";
        }
        if (!empty($param)) {
            $para = substr($para,0,strlen($para)-1);
        }

        return $this -> multipage($total, $page, $para);
    }

    /**
     * @param $maxpage  总页数
     * @param $page    当前页
     * @param string $para  翻页参数(不需要写$page),如http://www.example.com/article.php?page=3&id=1，$para参数就应该设为'&id=1'
     * @return string  返回的输出分页html内容
     */

    private function multipage($totalCount, $page, $para = '') {
        $multipage = '';  //输出的分页内容
        $listnum = 5;     //同时显示的最多可点击页面

        $maxpage = ceil($totalCount/ $this->config->item('pageSize'));
        if ($maxpage < 2) {
            return '';
        }else{
            $offset = 2;
            if ($maxpage <= $listnum) {
                $from = 1;
                $to = $maxpage;
            } else {
                $from = $page - $offset; //起始页
                $to = $from + $listnum - 1;  //终止页
                if($from < 1) {
                    $to = $page + 1 - $from;
                    $from = 1;
                    if($to - $from < $listnum) {
                        $to = $listnum;
                    }
                } elseif($to > $maxpage) {
                    $from = $maxpage - $listnum + 1;
                    $to = $maxpage;
                }
            }

            $multipage .= ($page - $offset > 1 && $maxpage >= $page ? '<li><a href="?page=1'.$para.'" >1...</a></li>' : '').
                ($page > 1 ? '<li><a href="?page='.($page - 1).$para.'" >&laquo;</a></li>' : '');

            for($i = $from; $i <= $to; $i++) {
                $multipage .= $i == $page ? '<li class="active"><a href="?page='.$i.$para.'" >'.$i.'</a></li>' : '<li><a href="?page='.$i.$para.'" >'.$i.'</a></li>';
            }

            $multipage .= ($page < $maxpage ? '<li><a href="?page='.($page + 1).$para.'" >&raquo;</a></li>' : '').
                ($to < $maxpage ? '<li><a href="?page='.$maxpage.$para.'" class="last" >...'.$maxpage.'</a></li>' : '');
            //$multipage .=  ' <li><a href="#" ><input type="text" size="3"  onkeydown="if(event.keyCode==13) {self.window.location=\'?page=\'+this.value+\''.$para.'\'; return false;}" ></a></li>';


            $multipage = $multipage ? '<ul class="pagination">'.$multipage.'</ul>' : '';
        }

        return $multipage;
    }
}